package com.ereport.controller.DARController;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.AccountsDAO;
import com.ereport.dao.DailyActivityDAO;
import com.ereport.dao.OpportunityDAO;
import com.ereport.entity.AccContacts;
import com.ereport.entity.Accounts;
import com.ereport.entity.DailyActivity;
import com.ereport.entity.Executives;
import com.ereport.entity.Opportunity;

@Controller
@RequestMapping("/dailyCallReport")
public class DailyActReportController {
	@RequestMapping(value="/open.do",method=RequestMethod.GET)
	public String open(HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			
			return "/jsp/login.jsp";
		}
		DailyActivity da=new DailyActivity();
		/*DailyActivityDAO dadao=new DailyActivityDAO();
		Set<String> daset=new HashSet<String>();
		List<Accounts> dalist=new ArrayList<Accounts>();
		StringBuilder sb=new StringBuilder();
		try {
			dalist = dadao.getOrgList(ex.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(dalist!=null){
		for(int i=0;i<dalist.size();i++){
			String org=dalist.get(i).getAccName();
			daset.add(org);
		}
		
		Iterator itr=daset.iterator();
		while(itr.hasNext()){
			Object obj=itr.next();
			String or=(String)obj;
			if(or.length()!=0){
			sb.append(or+"/");	
			}
					
		}
		}*/
		model.addAttribute("dailyAct", da);
		//model.addAttribute("orglist", sb.toString());
		return "/jsp/dailycall.jsp";
		
	}
	
	@RequestMapping(value="/save.do",method = RequestMethod.POST)
	public  ModelAndView insert(  HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String rCount=req.getParameter("rCount");
		int rc=Integer.parseInt(rCount);
		System.out.println("row count "+rCount);
		Date dt=null;
		String edate="";
		for(int r=1;r<=rc;r++){
			String org=req.getParameter("org"+r);
			String desgn=req.getParameter("disig"+r);
			String contper=req.getParameter("contper"+r);
			String contmbl=req.getParameter("mbl"+r);
			
			String contemail=req.getParameter("email"+r);
			String contfax=req.getParameter("fax"+r);
			String contlandph=req.getParameter("lndPhn"+r);
			String visitObj=req.getParameter("vistObj"+r);
			System.out.println("visit object "+visitObj);
			String visitOC=req.getParameter("vistOC"+r);
			String nststep=req.getParameter("nst"+r);
			String kms=req.getParameter("kms"+r);
			edate=req.getParameter("date");
			DateFormat format=new SimpleDateFormat("dd-MM-yyyy");
			dt=format.parse(edate);
			
			System.out.println("organistion "+org);
			if(org!=null){
				if(!org.equals("")){
				int km=0;
				if(kms.length()!=0){
					km=Integer.parseInt(kms);
				}
				DailyActivityDAO da=new DailyActivityDAO();
				da.insert(new DailyActivity(ex,org,desgn,contper,contmbl,contemail,contfax,contlandph,visitObj,visitOC,nststep,km,new java.sql.Date(dt.getTime())));
			}
			}
		}
		model.setViewName("redirect:/DailyCallReport/getActivity.do");
		model.addObject("dailyDate",edate);
		return model;
		
	}
	
	@RequestMapping("/getDivision.do")
	public @ResponseBody
	String getDiv(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			return "";
		}
		String org=req.getParameter("org");
		AccountsDAO acdao=new AccountsDAO();
		List<AccContacts> aContList=new ArrayList<AccContacts>();
		try {
			aContList= acdao.getAccContList(ex.getId(), org);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aContList!=null){
		Set<String> acDivSet=new HashSet<String>();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<aContList.size();i++){
			acDivSet.add(aContList.get(i).getPin());
		}
		if(acDivSet!=null){
		Iterator itr=acDivSet.iterator();
		while(itr.hasNext()){
			Object obj=itr.next();
			String st=(String)obj;
			sb.append(st+"/");
		}
		}
		return sb.toString();
		}else {
			return "";
		}
	}
	@RequestMapping("/getContPerson.do")
	public @ResponseBody
	String getContPerson(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			return "";
		}
		String org=req.getParameter("org");
		String div=req.getParameter("div");
		AccountsDAO acdao=new AccountsDAO();
		List<AccContacts> aContList=new ArrayList<AccContacts>();
		try {
			aContList= acdao.getAccContPers(ex.getId(),org,div);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aContList!=null){
			Set<String> acDivSet=new HashSet<String>();
			StringBuilder sb=new StringBuilder();
			for(int i=0;i<aContList.size();i++){
				acDivSet.add(aContList.get(i).getContactName());
			}
			if(acDivSet!=null){
				Iterator itr=acDivSet.iterator();
				while(itr.hasNext()){
					Object obj=itr.next();
					String st=(String)obj;
					sb.append(st+"/");
				}
			}
			return sb.toString();
		}else {
			return "";
		}
	}
	@RequestMapping("/getContPersonDet.do")
	public @ResponseBody
	String getContPersonDet(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			return "";
		}
		String org=req.getParameter("org");
		String div=req.getParameter("div");
		String contper=req.getParameter("contper");
		
		AccountsDAO acdao=new AccountsDAO();
		Accounts acc=new Accounts();
		try {
			acc=acdao.getAcc(ex.getId(), org);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		AccContacts aCont=new AccContacts();
		try {
			aCont= acdao.getAccContPerdet(ex.getId(),org,div,contper);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aCont!=null){
			StringBuilder sb=new StringBuilder();
			
			sb.append(acc.getFax()+"/");
			sb.append(aCont.getEmail()+"/");
			sb.append(aCont.getPhone()+"/");
			return sb.toString();
		}else {
			return "";
		}
	}
	@RequestMapping("/getCompany.do")
	public @ResponseBody
	String getCompany(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			return "";
		}
		DailyActivityDAO dadao=new DailyActivityDAO();
		Set<String> daset=new HashSet<String>();
		List<Accounts> dalist=new ArrayList<Accounts>();
		StringBuilder sb=new StringBuilder();
		try {
			dalist = dadao.getOrgList(ex.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(dalist!=null){
		for(int i=0;i<dalist.size();i++){
			String org=dalist.get(i).getAccName();
			daset.add(org);
		}
		
		Iterator itr=daset.iterator();
		while(itr.hasNext()){
			Object obj=itr.next();
			String or=(String)obj;
			if(or.length()!=0){
			sb.append(or+"/");	
			}
					
		}
		return sb.toString();
		}else{
		return "";
		}
	}
	
}
